package com.satan.controller;

import com.satan.dto.PageDto;
import com.satan.dto.UserPwdDto;
import com.satan.entity.SysUser;
import com.satan.entity.response.ResCode;
import com.satan.entity.response.ResultEntity;
import com.satan.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO
 * @date 2023/10/4 1:45
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class SysUserController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 添加或修改用户
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:user:add')" + "||" + "hasAuthority('system:user:edit')")
    public ResultEntity saveOrUpdateUser(@RequestBody SysUser sysUser) {
        boolean b = sysUserService.saveOrUpdateUser(sysUser, bCryptPasswordEncoder);
        if (b) {
            return ResultEntity.success(null);
        } else {
            return ResultEntity.fail(ResCode.UPDATE_ERROR);
        }

    }

    /**
     * 修改密码
     */
    @PostMapping("/update/pwd")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResultEntity updateUserPwd(@RequestBody UserPwdDto userPwdDto) {
        System.out.println("userPwdDto = " + userPwdDto);
        ResCode resCode = sysUserService.updateUserPwd(userPwdDto, bCryptPasswordEncoder);
        if (resCode != null && resCode.getCode() != 200) {
            return ResultEntity.fail(resCode);
        } else {
            return ResultEntity.success(resCode);
        }


    }

    /**
     * 上传用户头像
     */
    @PostMapping("/upload/avatar")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResultEntity uploadUserAvatar(MultipartFile file) throws IOException {
        Map<String, Object> resultMap = sysUserService.uploadAvatar(file);
        return ResultEntity.success(resultMap);
    }

    /**
     * 修改用户头像
     *
     * @param sysUser
     * @return
     */
    @RequestMapping("/update/avatar")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResultEntity updateAvatar(@RequestBody SysUser sysUser) {
        boolean b = sysUserService.updateAvatar(sysUser);
        if (b) {
            return ResultEntity.success(null);
        } else {
            return ResultEntity.fail(ResCode.UPDATE_ERROR);
        }

    }

    /**
     * 根据条件分页查询用户信息
     *
     * @param pageDto
     * @return
     */
    @PostMapping("/list/page")
    @PreAuthorize("hasAuthority('system:user:query')")
    public ResultEntity pageList(@RequestBody PageDto pageDto) {
        Map<String, Object> resultMap = sysUserService.getPageList(pageDto);
        return ResultEntity.success(resultMap);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:user:query')")
    public ResultEntity findById(@PathVariable(value = "id") Integer id) {
        SysUser sysUser = sysUserService.getById(id);
        return ResultEntity.success(sysUser);
    }

    /**
     * 验证用户名
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/check/name")
    @PreAuthorize("hasAuthority('system:user:query')")
    public ResultEntity checkUserName(@RequestBody SysUser sysUser) {
        SysUser sysUser2 = sysUserService.getUserByName(sysUser.getUsername());
        if (sysUser2 != null) {
            return ResultEntity.fail(ResCode.USER_NAME_EXIST);
        } else {
            return ResultEntity.success(null);

        }
    }

    /**
     * 删除和批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('system:user:delete')")
    public ResultEntity deleteUserByIds(@RequestBody Long[] ids) {
        boolean b = sysUserService.deleteUserByIds(ids);
        if (b) {
            return ResultEntity.success(null);
        } else {
            return ResultEntity.fail(ResCode.DELETE_ERROR);
        }

    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @GetMapping("/reset/pwd/{id}")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResultEntity resetPassword(@PathVariable(value = "id") Integer id) {
        boolean b = sysUserService.resetPassword(id, bCryptPasswordEncoder);
        if (b) {
            return ResultEntity.success(null);
        } else {
            return ResultEntity.fail(ResCode.UPDATE_ERROR);
        }
    }

    /**
     * 更新status状态
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/update/status/{id}/{status}")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResultEntity updateStatus(@PathVariable(value = "id") Integer id, @PathVariable(value
            = "status") String status) {
        boolean b = sysUserService.updateStatus(id, status);
        if (b) {
            return ResultEntity.success(null);
        } else {
            return ResultEntity.fail(ResCode.UPDATE_ERROR);
        }
    }


}
